﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CSP.Receipts.Business;
using Ninject;
using CSP.Receipts.DataModel;
namespace MySharpReceipts.Controllers
{
    public class MgmtController : SecureBaseController
    {
        IReceiptRepository _receiptRepository;
        public MgmtController(IReceiptRepository receiptRepos)
        {
            _receiptRepository = receiptRepos;
        }

        public ActionResult Index()
        {
            return View(_receiptRepository.GetReceiptsByUser(CurrentUser.Id));
        }

        //
        // GET: /Mgmt/Details/5

        public ActionResult Details(int id)
        {
            var result = _receiptRepository.GetOne(rec => rec.Id == id);
            return View(result);
        }

        //
        // GET: /Mgmt/Create

        public ActionResult Create()
        {
            var repos = Kernel.Get<ICategoryRepository>();
            LoadCategories();
            return View();
        } 

        //
        // POST: /Mgmt/Create

        [HttpPost]
        public ActionResult Create(Receipt receipt, int categoryId)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    receipt.CreationDate = DateTime.Now;
                    receipt.CategoryId = categoryId;
                    receipt.OwnerId = CurrentUser.Id;
                    receipt.ReceiptPictures = new List<Picture>();
                    _receiptRepository.Insert(receipt);

                    return RedirectToAction("Index");
                }
                else
                {
                    LoadCategories();
                    return View(receipt);
                }
            }
            catch(Exception ex) 
            {
                LoadCategories();
                return View(receipt);
            }
        }
        
        //
        // GET: /Mgmt/Edit/5
 
        public ActionResult Edit(int id)
        {
            LoadCategories();
            return View(_receiptRepository.GetOne(rec => rec.Id == id));
        }

        //
        // POST: /Mgmt/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Receipt receipt)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                LoadCategories();
                return View();
            }
        }

        //
        // GET: /Mgmt/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(_receiptRepository.GetOne(rec => rec.Id == id));
        }

        //
        // POST: /Mgmt/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, Receipt receipt)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        private void LoadCategories()
        {
            var repos = Kernel.Get<ICategoryRepository>();
            ViewBag.Categories = repos.GetAll().OrderBy(cat => cat.Title);
        }
    }
}
